package com.apinji.alipay.common;

import com.apinji.alipay.protocol.AlipayTradeResponse;
import com.apinji.common.plugin.StrUtils;

/**
 * Created by 轩辕夏夏 on 2017/7/6.
 */
public enum AlipayErrorCodeEnum {
    SUCCESS(10000, "", "接口调用成功，调用结果请参考具体的API文档所对应的业务返回参数"),


    SERVER_UNAVAILABLE(20000, "", "服务不可用"),
    SERVER_UNAVAILABLE_ISP(20000, "isp.unknow-error", "服务暂不可用（业务系统不可用）"),
    SERVER_UNAVAILABLE_AOP(20000, "aop.unknow-error", "服务暂不可用（网关自身的未知错误）"),


    INVALID_AUTH(20001, "", "授权权限不足"),
    INVALID_AUTH_TOKEN(20001, "aop.invalid-auth-token", "无效的访问令牌"),
    AUTH_TOKEN_TIME_OUT(20001, "aop.auth-token-time-out", "访问令牌已过期"),
    INVALID_APP_AUTH_TOKEN(20001, "aop.invalid-app-auth-token", "无效的应用授权令牌"),
    INVALID_APP_AUTH_TOKEN_NO_API(20001, "aop.invalid-app-auth-token-no-api", "商户未授权当前接口"),
    APP_AUTH_TOKEN_TIME_OUT(20001, "aop.app-auth-token-time-out", "应用授权令牌已过期"),
    NO_PRODUCT_REG_BY_PARTNER(20001, "aop.no-product-reg-by-partner", "商户未签约任何产品"),


    MISSING(40001, "", "缺少必选参数"),
    MISSING_METHOD(40001, "isv.missing-method", "缺少方法名参数"),
    MISSING_SIGNATURE(40001, "isv.missing-signature", "缺少签名参数"),
    MISSING_SIGNATURE_TYPE(40001, "isv.missing-signature-type", "缺少签名类型参数"),
    MISSING_SIGNATURE_KEY(40001, "isv.missing-signature-key", "缺少签名配置"),
    MISSING_APP_ID(40001, "isv.missing-app-id", "缺少appId参数"),
    MISSING_TIMESTAMP(40001, "isv.missing-timestamp", "缺少时间戳参数"),
    MISSING_VERSION(40001, "isv.missing-version", "缺少版本参数"),
    DECRYPTION_ERROR_MISSING_ENCRYPT_TYPE(40001, "isv.decryption-error-missing-encrypt-type", "解密出错, 未指定加密算法"),

    INVALID_ERROR(40002, "", "非法的参数"),
    INVALID_PARAMETER(40002, "isv.invalid-parameter", "参数无效"),
    UPLOAD_FAIL(40002, "isv.upload-fail", "文件上传失败"),
    INVALID_FILE_EXTENSION(40002, "isv.invalid-file-extension", "文件扩展名无效"),
    INVALID_FILE_SIZE(40002, "isv.invalid-file-size", "文件大小无效"),
    INVALID_METHOD(40002, "isv.invalid-method", "不存在的方法名"),
    INVALID_FORMAT(40002, "isv.invalid-format", "无效的数据格式"),
    INVALID_SIGNATURE_TYPE(40002, "isv.invalid-signature-type", "无效的签名类型"),
    INVALID_SIGNATRUE(40002, "isv.invalid-signature", "无效签名"),
    INVALID_ENCRYPT_TYPE(40002, "isv.invalid-encrypt-type", "无效的加密类型"),
    INVALID_ENCRYPT(40002, "isv.invalid-encrypt", "解密异常"),
    INVALID_APP_ID(40002, "isv.invalid-app-id", "无效的appId参数"),
    INVALID_TIMESTAMP(40002, "isv.invalid-timestamp", "非法的时间戳参数"),
    INVALID_CHARSET(40002, "isv.invalid-charset", "字符集错误"),
    INVALID_DIGEST(40002, "isv.invalid-digest", "摘要错误"),
    DECRYPTION_ERROR_NOT_VALID_ENCRYPT_TYPE(40002, "isv.decryption-error-not-valid-encrypt-type", "解密出错，不支持的加密算法"),
    DECRYPTION_ERROR_NOT_VALID_ENCRYPT_KEY(40002, "isv.decryption-error-not-valid-encrypt-key", "解密出错, 未配置加密密钥或加密密钥格式错误"),
    DECRYPTION_RRROR_UNKOWN(40002, "isv.decryption-error-unknown", "解密出错，未知异常"),
    MISSING_SIGNATURE_CONFIG(40002, "isv.missing-signature-config", "验签出错, 未配置对应签名算法的公钥或者证书"),
    NOT_SUPPORT_APP_AUTH(40002, "isv.not-support-app-auth", "本接口不支持第三方代理调用"),

    BUSINESS_PROCESS_FAILURE(40004, "", "业务处理失败"),


    //--------------------退款业务错误码
    SYSTEM_ERROR(40004, "ACQ.SYSTEM_ERROR", "系统错误"),
    //INVALID_PARAMETER(40004, "ACQ.INVALID_PARAMETER", "参数无效"),
    SELLER_BALANCE_NOT_ENOUGH(40004, "ACQ.SELLER_BALANCE_NOT_ENOUGH", "卖家余额不足"),
    REFUND_AMT_NOT_EQUAL_TOTAL(40004, "ACQ.REFUND_AMT_NOT_EQUAL_TOTAL", "退款金额超限"),
    REASON_TRADE_BEEN_FREEZEN(40004, "ACQ.REASON_TRADE_BEEN_FREEZEN", "请求退款的交易被冻结"),
    TRADE_NOT_EXIST(40004, "ACQ.TRADE_NOT_EXIST", "交易不存在"),
    TRADE_HAS_FINISHED(40004, "ACQ.TRADE_HAS_FINISHED", "交易已完结"),
    TRADE_STATUS_ERROR(40004, "ACQ.TRADE_STATUS_ERROR", "交易状态非法"),
    DISCORDANT_REPEAT_REQUEST(40004, "ACQ.DISCORDANT_REPEAT_REQUEST", "不一致的请求"),
    REASON_TRADE_REFUND_FEE_ERR(40004, "ACQ.REASON_TRADE_REFUND_FEE_ERR", "退款金额无效"),
    TRADE_NOT_ALLOW_REFUND(40004, "ACQ.TRADE_NOT_ALLOW_REFUND", "当前交易不允许退款"),


    //-------------------支付业务错误码
    //SYSTEM_ERROR(40004, "ACQ.SYSTEM_ERROR", "接口返回错误"),
    //INVALID_PARAMETER(40004, "ACQ.INVALID_PARAMETER	", "参数无效"),
    ACCESS_FORBIDDEN(40004, "ACQ.ACCESS_FORBIDDEN", "无权限使用接口"),
    EXIST_FORBIDDEN_WORD(40004, "ACQ.EXIST_FORBIDDEN_WORD", "订单信息中包含违禁词"),
    PARTNER_ERROR(40004, "ACQ.PARTNER_ERROR", "应用APP_ID填写错误"),
    TOTAL_FEE_EXCEED(40004, "ACQ.TOTAL_FEE_EXCEED", "订单总金额超过限额"),
    PAYMENT_AUTH_CODE_INVALID(40004, "ACQ.PAYMENT_AUTH_CODE_INVALID", "支付授权码无效"),
    CONTEXT_INCONSISTENT(40004, "ACQ.CONTEXT_INCONSISTENT	", "交易信息被篡改"),
    TRADE_HAS_SUCCESS(40004, "ACQ.TRADE_HAS_SUCCESS	", "交易已被支付	"),
    TRADE_HAS_CLOSE(40004, "ACQ.TRADE_HAS_CLOSE	", "交易已经关闭	更换商家订单号后，重新发起请求"),
    BUYER_BALANCE_NOT_ENOUGH(40004, "ACQ.BUYER_BALANCE_NOT_ENOUGH", "	买家余额不足"),
    BUYER_BANKCARD_BALANCE_NOT_ENOUGH(40004, "ACQ.BUYER_BANKCARD_BALANCE_NOT_ENOUGH", "用户银行卡余额不足"),
    ERROR_BALANCE_PAYMENT_DISABLE(40004, "ACQ.ERROR_BALANCE_PAYMENT_DISABLE", "余额支付功能关闭"),
    BUYER_SELLER_EQUAL(40004, "ACQ.BUYER_SELLER_EQUAL", "买卖家不能相同"),
    TRADE_BUYER_NOT_MATCH(40004, "ACQ.TRADE_BUYER_NOT_MATCH", "交易买家不匹配"),
    BUYER_ENABLE_STATUS_FORBID(40004, "ACQ.BUYER_ENABLE_STATUS_FORBID", "买家状态非法"),
    PULL_MOBILE_CASHIER_FAIL(40004, "ACQ.PULL_MOBILE_CASHIER_FAIL", "唤起移动收银台失败"),
    MOBILE_PAYMENT_SWITCH_OFF(40004, "ACQ.MOBILE_PAYMENT_SWITCH_OFF", "用户的无线支付开关关闭"),
    PAYMENT_FAIL(40004, "ACQ.PAYMENT_FAIL", "支付失败"),
    BUYER_PAYMENT_AMOUNT_DAY_LIMIT_ERROR(40004, "ACQ.BUYER_PAYMENT_AMOUNT_DAY_LIMIT_ERROR", "买家付款日限额超限"),
    BEYOND_PAY_RESTRICTION(40004, "ACQ.BEYOND_PAY_RESTRICTION", "商户收款额度超限"),
    BEYOND_PER_RECEIPT_RESTRICTION(40004, "ACQ.BEYOND_PER_RECEIPT_RESTRICTION", "商户收款金额超过月限额"),
    BUYER_PAYMENT_AMOUNT_MONTH_LIMIT_ERROR(40004, "ACQ.BUYER_PAYMENT_AMOUNT_MONTH_LIMIT_ERROR", "买家付款月额度超限"),
    SELLER_BEEN_BLOCKED(40004, "ACQ.SELLER_BEEN_BLOCKED", "商家账号被冻结"),
    ERROR_BUYER_CERTIFY_LEVEL_LIMIT(40004, "ACQ.ERROR_BUYER_CERTIFY_LEVEL_LIMIT", "买家未通过人行认证"),
    PAYMENT_REQUEST_HAS_RISK(40004, "ACQ.PAYMENT_REQUEST_HAS_RISK", "支付有风险"),
    NO_PAYMENT_INSTRUMENTS_AVAILABLE(40004, "ACQ.NO_PAYMENT_INSTRUMENTS_AVAILABLE", "没用可用的支付工具"),
    USER_FACE_PAYMENT_SWITCH_OFF(40004, "ACQ.USER_FACE_PAYMENT_SWITCH_OFF", "用户当面付付款开关关闭"),
    INVALID_STORE_ID(40004, "ACQ.INVALID_STORE_ID", "商户门店编号无效"),
    SUB_MERCHANT_CREATE_FAIL(40004, "ACQ.SUB_MERCHANT_CREATE_FAIL", "级商户创建失败"),
    SUB_MERCHANT_TYPE_INVALID(40004, "ACQ.SUB_MERCHANT_TYPE_INVALID", "二级商户类型非法"),
    AGREEMENT_NOT_EXIST(40004, "ACQ.AGREEMENT_NOT_EXIST", "用户协议不存在"),
    AGREEMENT_INVALID(40004, "ACQ.AGREEMENT_INVALID", "用户协议失效"),
    AGREEMENT_STATUS_NOT_NORMAL(40004, "ACQ.AGREEMENT_STATUS_NOT_NORMAL", "用户协议状态非NORMAL	"),
    MERCHANT_AGREEMENT_NOT_EXIST(40004, "ACQ.MERCHANT_AGREEMENT_NOT_EXIST", "	商户协议不存在"),
    MERCHANT_AGREEMENT_INVALID(40004, "ACQ.MERCHANT_AGREEMENT_INVALID", "商户协议已失效"),
    MERCHANT_STATUS_NOT_NORMAL(40004, "ACQ.MERCHANT_STATUS_NOT_NORMAL", "商户协议状态非正常状态"),


    //----------------------支付查询业务错误
    //SYSTEM_ERROR(40004, "ACQ.SYSTEM_ERROR", "系统错误"),
    //INVALID_PARAMETER(40004, "ACQ.INVALID_PARAMETER", "参数无效"),
    //TRADE_NOT_EXIST(40004, "ACQ.TRADE_NOT_EXIST", "查询的交易不存在"),


    //---------------------关闭支付 业务错误
    TRADE_CLOSED(40004, "tradeStatus.TRADE_CLOSED", "交易关闭"),
    TRADE_SUCCESS(40004, "tradeStatus.TRADE_SUCCESS", "交易成功"),




    INSUFFICIENT_PERMISSIONS(40006, "", "权限不足"),
    INSUFFICIENT_ISV_PERMISSIONS(40006, "isv.insufficient-isv-permissions", "ISV权限不足"),
    INSUFFICIENT_UER_PERMISSIONS(40006, "isv.insufficient-user-permissions", "用户权限不足")


    ;


    private int code;
    private String subCode;
    private String msg;

    AlipayErrorCodeEnum(int code, String subCode, String msg){
        this.code = code;
        this.subCode = subCode;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getSubCode() {
        return subCode;
    }

    public void setSubCode(String subCode) {
        this.subCode = subCode;
    }

    /**
     * 根据错误码，获得错误信息
     * @param code
     * @return
     */
    public static String getMsg(int code, String subCode){
        for(int i = 0; i < AlipayErrorCodeEnum.values().length; i++) {
            if (AlipayErrorCodeEnum.values()[i].getCode() == code && AlipayErrorCodeEnum.values()[i].getSubCode().equalsIgnoreCase(subCode)) {
                return "["+ code + "]" + AlipayErrorCodeEnum.values()[i].getMsg();
            }
        }
        return "Error code not found [" + code + ":" + subCode + "]";
    }

    public static String getMsg(AlipayTradeResponse response) {
        int code = response.getCode();
        String subCode = response.getSubCode();
        return getMsg(code, subCode);
    }
}
